package com.mapbased.wfw.bind;

import com.mapbased.wfw.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy(Baily)
 * @version 1.0
 */
public class PageBinding
    extends AbstractBinding
{
    // private String attributeName;
    private java.lang.reflect.Method method;
    private boolean isContext;
    public PageBinding(String exp)
    {
        super(exp);
        //   this.attributeName=attributeName;
    }

    /**
     * getValue
     *
     * @param context RenderContext
     * @return Object
     * @todo Implement this com.mapbased.wfw.Binding method
     */
    protected Object getDirectValue(RenderContext context)
    {
        try
        {

            PageHandler handler=context.getPageHandler();
            if(method == null)
            {
                synchronized(this)
                {

                    try
                    {
                        method=handler.getClass().getMethod(this.
                            attributeName.trim() + "Binding",
                            new Class[]
                            {RenderContext.class});
                        this.isContext=true;

                    }

                    catch(NoSuchMethodException ex)
                    {
                        method=handler.getClass().getMethod(this.
                            attributeName.trim() + "Binding",
                            new Class[]
                            {java.util.Map.class});
                          this.isContext=false;

                    }
                }
            }

            Object o=context;
            if(!this.isContext)
            {
                o=context.getRequestMap();
            }
            return method.invoke(handler,new Object[]
                                 {o});
        }

        catch(Exception e)
        {
            throw new java.lang.RuntimeException(e);
        }
    }
}
